<template>
  <el-drawer
    :title="nickName+(user_id>0?'-订单信息':'-推荐订单信息')"
    :visible.sync="drawer_open"
    size="980px"
    append-to-body
  >
    <div class="user">
      <div class="common-seach-wrap">
        <el-form size="small" :inline="true" :model="searchForm" class="demo-form-inline">
          <el-form-item label="">
            <el-input size="small" v-model="searchForm.order_no" placeholder="请输入订单号"></el-input>
          </el-form-item>
          <el-form-item label="">
            <el-select size="small" v-model="searchForm.style_id" placeholder="配送方式">
              <el-option label="全部" value=""></el-option>
              <el-option v-for="(item, index) in exStyle" :key="index" :label="item.name"
                         :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="">
            <div class="block">
              <span class="demonstration"></span>
              <el-date-picker
                size="small"
                v-model="searchForm.create_time"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button size="small" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
            <el-button size="small" type="success" icon="el-icon-search" @click="onExport">导出</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="table-wrap">
        <el-tabs v-model="activeName" @tab-click="handleClick">
          <el-tab-pane label="全部订单" name="all"></el-tab-pane>
          <el-tab-pane :label="'未付款'" name="payment">
            <span slot="label">未付款 <el-tag size="mini">{{ order_count.payment }}</el-tag></span>
          </el-tab-pane>
          <el-tab-pane :label="'待发货'" name="delivery">
            <span slot="label">待发货 <el-tag size="mini">{{ order_count.delivery }}</el-tag></span>
          </el-tab-pane>
          <el-tab-pane :label="'待收货'" name="received">
            <span slot="label">待收货 <el-tag size="mini">{{ order_count.received }}</el-tag></span>
          </el-tab-pane>
          <el-tab-pane :label="'已取消'" name="canceled">
            <span slot="label">已取消 <el-tag size="mini">{{ order_count.canceled }}</el-tag></span>
          </el-tab-pane>
          <el-tab-pane label="待评价" name="comment"></el-tab-pane>
          <el-tab-pane label="已完成" name="complete"></el-tab-pane>
          <el-tab-pane :label="'待开票'" name="is_invoice">
            <span slot="label">待开票 <el-tag size="mini">{{ order_count.is_invoice }}</el-tag></span>
          </el-tab-pane>
          <el-tab-pane :label="'已开票'" name="is_invoiced">
            <span slot="label">已开票 <el-tag size="mini">{{ order_count.is_invoiced }}</el-tag></span>
          </el-tab-pane>
          <!--          <el-tab-pane :label="'待取消'" name="cancel">-->
          <!--            <span slot="label">待取消 <el-tag size="mini">{{order_count.cancel}}</el-tag></span>-->
          <!--          </el-tab-pane>-->
         
        </el-tabs>
        <el-table size="small" :data="tableData.data" :span-method="arraySpanMethod" border height="calc(100vh - 280px)"
                  style="width: 100%"
                  v-loading="loading">
          <el-table-column prop="order_no" label="订单信息" width="400">
            <template slot-scope="scope">
              <div class="order-code" v-if="scope.row.is_top_row">
                <span class="state-text"
                      :class="{'state-text-red':scope.row.order_source != 10}">{{ scope.row.order_source_text }}</span>
                <span v-if="scope.row.is_first_order" class="state-text state-text-red">首单</span>
                <span class="c_main">订单号：{{ scope.row.order_no }}</span>
                <span class="pl16">下单时间：{{ scope.row.create_time }}</span>
                <!--是否取消申请中-->
                <span class="pl16" v-if="scope.row.order_status == 21"><el-tag effect="dark"
                                                                               size="mini">取消申请中</el-tag></span>
              </div>
              <template v-else>
                <div class="product-info" v-for="(item, index) in scope.row.product" :key="index">
                  <div class="pic"><img v-img-url="item.image.file_path" alt=""/></div>
                  <div class="info">
                    <div class="name gray3 product-name">
                      <span>{{ item.product_name }}</span>
                    </div>
                    <div class="gray9" v-if="item.product_attr">{{ item.product_attr }}</div>
                  </div>
                  <div class="d-c-c d-c">
                    <div class="orange">￥ {{ item.product_price }}</div>
                    <div class="gray3">x{{ item.total_num }}</div>
                  </div>
                </div>
              </template>

              <template v-if="scope.row.is_order_refund > 0">
                <div class="name gray3 product-name">
                  <span v-if="scope.row.refund_type == 10" style="color: red">售后：退货退款</span>
                  <span v-else-if="scope.row.refund_type == 30" style="color: red">售后：仅退款</span>
                </div>
              </template>
            </template>
          </el-table-column>

          <el-table-column prop="pay_price" label="赠品">
            <template slot-scope="scope" v-if="scope.row.gift_product_order">
              <div class="pic"><img v-img-url="scope.row.gift_product_order.file.file_path" style="width: 60px;"
                                    alt=""/></div>
              <span>{{ scope.row.gift_product_order.gift_product.product_name }}</span>

            </template>
          </el-table-column>


          <el-table-column prop="pay_price" label="实付款">
            <template slot-scope="scope" v-if="!scope.row.is_top_row">
              <div class="orange" v-if="scope.row.order_source!=70">{{ scope.row.pay_price }}</div>
              <p class="orange" v-if="scope.row.order_source==70">定金：{{ scope.row.advance.pay_price }}</p>
              <p class="orange" v-if="scope.row.order_source==70">尾款：{{ scope.row.pay_price }}</p>
              <p class="gray9">(含运费：{{ scope.row.express_price }})</p>
            </template>
          </el-table-column>
          <el-table-column prop="supplier.name" label="商户名称"></el-table-column>
          <el-table-column prop="state_text" label="交易状态">
            <template slot-scope="scope" v-if="!scope.row.is_top_row">
              {{ scope.row.state_text }}
            </template>
          </el-table-column>
          <!--          <el-table-column prop="pay_type.text" label="支付方式">-->
          <!--            <template slot-scope="scope" v-if="!scope.row.is_top_row">-->
          <!--              <span class="gray9">{{ scope.row.pay_type.text }}</span>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column prop="store" label="配送仓库">
            <template slot-scope="scope" v-if="scope.row.store != null && scope.row.store != undefined">
              <div>{{ scope.row.store.store_name }}</div>
            </template>
          </el-table-column>
          <el-table-column prop="store" label="配送员" style="width: 100px;">
            <template slot-scope="scope" v-if="scope.row.clerk != null && scope.row.clerk != undefined">
              <div>{{ scope.row.clerk.real_name }}</div>
              <div>{{ scope.row.clerk.mobile }}</div>
            </template>
          </el-table-column>
          <!--          <el-table-column prop="delivery_type.text" label="配送方式">-->
          <!--            <template slot-scope="scope" v-if="!scope.row.is_top_row">-->
          <!--              <span class="green">{{ scope.row.delivery_type.text }}</span>-->
          <!--              <span v-if="scope.row.delivery_type.value == 30">手机号:{{ scope.row.user.mobile }}</span>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <el-table-column prop="is_comment" label="评价" width="60">
            <template slot-scope="scope" v-if="!scope.row.is_top_row">
              <span v-if="scope.row.is_comment == 0">未评价</span>
              <span v-else="">已评价</span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="160">

            <template slot-scope="scope" v-if="!scope.row.is_top_row">
              <el-button v-if="scope.row.is_invoice == 1"
                         @click="openInvoice(scope.row)" type="text"
                         size="small">订单开票
              </el-button>
              <el-button @click="addClick(scope.row)" type="text" size="small" v-auth="'/order/order/detail'">订单详情
              </el-button>
              <!--              <el-button v-if="scope.row.delivery_type.value==10&&scope.row.delivery_status.value==20" @click="getLogistics(scope.row)" type="text" size="small">查看物流</el-button>-->
            </template>
          </el-table-column>
        </el-table>
      </div>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
      <order-detail :show-detail.sync="showDetail" :order_id="order_id"></order-detail>
    </div>
  </el-drawer>
</template>
<script>
import * as XLSX from "xlsx";
import OrderApi from "@/api/order";

import OrderDetail from "./OrderDetail.vue";

export default {
  components: {
    OrderDetail
  },
  data() {
    return {
      activeName: 'all',
      drawer_open: false,
      /*是否加载完成*/
      loading: true,
      /*列表数据*/
      tableData: [],
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1,
      searchForm: {
        search: '',
        order_no: '',
        style_id: '',
        create_time: ''
      },
      order_count: {
        payment: 0,
        delivery: 0,
        received: 0,
        canceled: 0,
        is_invoice: 0,
        is_invoiced: 0
      },
      isLogistics: false,
      logisticsData: {},
      exStyle: [],
      showDetail: false,
      order_id: 0
    }
  },
  props: ['open_orders', 'user_id', 'nickName', 'referee_id', 'shop_supplier_id'],
  watch: {
    drawer_open(value) {
      this.$emit('update:open_orders', value);
    },
    open_orders(value) {
      this.drawer_open = value;
    },
    user_id() {
      this.curPage = 0;
      this.getData();
    },
    referee_id() {
      this.curPage = 0;
      this.getData();
    },
    shop_supplier_id() {
      this.curPage = 0;
      this.getData();
    }
  },
  methods: {
    /*跨多列*/
    arraySpanMethod(row) {
      if (row.rowIndex % 2 == 0) {
        if (row.columnIndex === 0) {
          return [1, 8];
        }
      }
    },
    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.getData();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getData();
    },
    getData() {
      let self = this;
      let Params = this.searchForm;
      Params.user_id = this.user_id
      Params.referee_id = this.referee_id
      Params.shop_supplier_id = this.shop_supplier_id
      Params.dataType = self.activeName;
      Params.page = self.curPage;
      Params.export = 0;
      Params.list_rows = self.pageSize;
      self.loading = true;
      OrderApi.orderlist(Params, true)
        .then(res => {
          let list = [];
          for (let i = 0; i < res.data.list.data.length; i++) {
            let item = res.data.list.data[i];
            let topitem = {
              order_no: item.order_no,
              create_time: item.create_time,
              order_source: item.order_source,
              order_source_text: item.order_source_text,
              is_top_row: true,
              order_status: item.order_status.value,
              is_first_order: item.is_first_order,
            };
            list.push(topitem);
            list.push(item);
          }
          self.tableData.data = list;

          self.totalDataNumber = res.data.list.total;
          self.exStyle = res.data.ex_style;
          self.order_count = res.data.order_count.order_count;
          self.loading = false;
        })
        .catch(error => {
        });
    },
    /*搜索查询*/
    onSubmit() {
      this.curPage = 1;
      this.tableData = [];
      this.getData();
    },
    addClick(row) {
      this.order_id = row.order_id;
      this.showDetail = true;
    },
    /*切换菜单*/
    handleClick(tab, event) {
      let self = this;
      self.curPage = 1;
      self.getData();
    },
    onExport() {
      let self = this;
      let Params = this.searchForm;
      Params.user_id = this.user_id
      Params.referee_id = this.referee_id
      Params.shop_supplier_id = this.shop_supplier_id
      Params.dataType = self.activeName;
      Params.page = self.curPage;
      Params.list_rows = 100000;
      Params.export = 1;
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      });
      OrderApi.orderlist(Params, true).then(res => {
        let ws = XLSX.utils.aoa_to_sheet(res.data.aoa);
        let wb = XLSX.utils.book_new();
        ws["!merges"] = res.data.merges;
        Object.keys(ws).forEach((key) => {
          if (key.indexOf('!') < 0) {
            ws[key].s = {
              alignment: {//对齐方式
                horizontal: 'center',//水平居中
                vertical: 'center',//竖直居中
                // wrapText: true,//自动换行
              },
            }
          }
        })
        XLSX.utils.book_append_sheet(wb, ws, res.data.sheetname);
        XLSX.writeFile(wb, res.data.filename);
        loading.close();
      }).catch(() => {
        loading.close();
      })
    }
  }
}

</script>
<style scoped>
.user {
  padding: 20px;
  padding-top: 0;
}

.order-code .state-text {
  padding: 2px 4px;
  border-radius: 4px;
  background: #808080;
  color: #ffffff;
}

.order-code .state-text-red {
  background: red;
}
</style>
